package com.biz.primus.ms.oms.dao.po;


import com.biz.primus.model.oms.enums.*;
import com.biz.primus.model.oms.vo.OmsOrderVo;
import com.biz.primus.ms.base.jpa.po.BaseEntity;
import com.biz.primus.ms.oms.jpa.OrderOriginDataWithJSONConverter;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import org.hibernate.envers.Audited;
import org.hibernate.envers.NotAudited;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yanweijin
 * @date 2017/5/4
 */
@Entity
@Table(name = "oms_order",indexes = {
		@Index(name = "idx_order_code",columnList = "orderCode"),
		@Index(name = "idx_user_id",columnList = "userId"),
		@Index(name = "idx_depot_code",columnList = "depotCode"),
		@Index(name = "idx_status",columnList = "state,commentState"),
		@Index(name = "idx_create_timestamp",columnList = "createTimestamp"),
})
@Data
@Audited
public class OmsOrder extends BaseEntity {

	private static final long serialVersionUID = 1283669897776119424L;

	/** 订单来源  */
	@Column(length = 128, nullable = false)
	private String orderSource;
	/** 店铺编码*/
	@Column(length = 64)
	private String depotCode;

	/**特殊订单活动, 当标识==normal时不需要id*/
	@Column(length = 64)
	private String specialEventId;

	/**特殊订单标识*/
	@Enumerated(EnumType.STRING)
	private SpecialOrderMark specialOrderMark = SpecialOrderMark.normal;


	/** 订单编号*/
	@Column(length = 32, nullable = false, unique = true)
	private String orderCode;


	/**
	 * 原订单号,订单拆分(split)之后,记录原始订单id
	 */
	private String sourceOrderCode;
	/** 客户备注*/
	@Column(length = 200)
	private String remark;
	/** 商户自备注(一般用于客服)*/
	@Column(length = 200)
	private String selfRemark;

	/**
	 * 订单渠道(编号)
	 */
	private String channel;

	@Version
	@Setter(AccessLevel.PRIVATE)
	private int version;

	/**
	 * 拆单次数
	 */
	private int splitCount = 0;

	/**
	 * 商品项
	 */
	@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
	@NotAudited
	private List<OmsOrderItem> items;
	/**
	 * 会员id
	 */
	private Long userId;

	/**
	 * 用户名称
	 */
	@Column(length = 200)
	private String userName;

	/**
	 * 用户电话
	 */
	@Column(length = 50)
	private String userMobile;

	/**
	 * 用户账户
	 */
	@Column(length = 100)
	private String userAccount;

	/**
	 * 门店名
	 */
	@Column(length = 50)
	private String depotName;

	/**
	 * 用户级别
	 */
	private Long userLevel;

	/**
	 * 最后自动分单/人工分单/人工拆单时间
	 */
	private LocalDateTime lastAllocationTime;

	/**
	 * 订单商品金额(商品计算折让之后的金额)
	 */
	private Integer orderAmount;
	/**
	 * 折让总金额
	 */
	private Integer discountAmount;
	/**
	 * 运费
	 */
	private Integer freightAmount;
	/**
	 * 已支付金额
	 */
	private Integer paidAmount;
	/**
	 * 下单ip
	 */
	private String orderingIp;

	/**
	 * 期望收货时间,预约送和自提有这个值,其他情况为空
	 */
	private LocalDateTime expectedReceivingTime;

	/**
	 * 期望的快递供应商
	 */
	private String expectedExpressProvider;

	/**
	 * 销售时间或预售时间
	 */
	private LocalDateTime saleTime;

	/**
	 * 全网收单系统下,原始订单数据json
	 */
	@Column(columnDefinition = "TEXT")
	@Convert(converter = OrderOriginDataWithJSONConverter.class)
	private OmsOrderVo originData;

	/**
	 * 收货人信息:
	 *  虽然订单和收货人信息的关系是1对多的(订单1,收货人多,因为每次后台修改收货人信息都会创建新的收货人记录)
	 *  但是为了系统方便,这里只记录有效的一条,并且在consignee一方记录orderId
	 */
	@ManyToOne(cascade = {CascadeType.PERSIST})
	@JoinColumn(name = "consignee_id")
	@NotAudited
	private OmsConsignee consignee;


	//------------订单状态---------------
	/**
	 * 订单取消标识
	 */
	@Enumerated(EnumType.STRING)
	private CancelType cancelType = CancelType.none;
	/**
	 * 评价状态
	 */
	@Enumerated(EnumType.STRING)
	private OrderCommentState commentState = OrderCommentState.unfinish;
	/**
	 * 配送状态
	 */
	@Convert(converter = OrderDeliveryState.Converter.class)
	private OrderDeliveryState deliveryState = OrderDeliveryState.for_outbound;
	/**
	 * 支付状态
	 */
	@Enumerated(EnumType.STRING)
	private OrderPaymentState paymentState;
	/**
	 * 订单状态
	 */
	@Convert(converter = OrderState.Converter.class)
	private OrderState state = OrderState.begin;
	/**
	 * 订单配送方式
	 */
	@Enumerated(EnumType.STRING)
	private OrderDeliveryType deliveryType;

	/**
	 * 是否有售后单
	 */
	private boolean inAfterSale = false;

	/**
	 * 调拨标识,为true时,门店不能主动申请改派,需要自行调拨商品,满足订单需求
	 */
	private boolean needDispatching = false;

//	@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
//	private List<OrderApportionment> apportionmentInfo;

	/**
	 * 快递单号
	 */
	@Column(length = 100)
	private String expressNumber;

	/**
	 * 第三方物流公司(当deliveryType为third_party时才有这个值)
	 */
	@Column(length = 50)
	private String expectedExpressProviderText;

	/**
	 * 安全码,门店发货时有这个值
	 */
	@Column(length = 50)
	private String safetyCode;

	/**
	 * 待分单类型: 人工分单,门店申请
	 */
	@Enumerated(EnumType.STRING)
	private AllocationType allocationType = AllocationType.none;

	/**
	 * 下单员工号(客服坐席/pos收银员), 自有渠道才有, app下单没有
	 */
	@Column(length = 50, nullable = false)
	private String operator = "";

	/**
	 * 配送时效 in19m, in29m, in59m
	 */
	@Column(length = 50)
	private String deliveryTimeliness;



}
